package com.nefu.store.common.result;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

//Http返回结果

@Data
@NoArgsConstructor
@AllArgsConstructor
public class HttpResult<T> {
    @Schema(description = "返回代码，200-成功，500-失败")
    private int code;  //返回代码 200 500

    @Schema(description = "返回信息")
    private String msg; //返回代码描述

    @Schema(description = "返回结果")
    private T result;  //返回结果

    @Schema(description = "返回时间")
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss",timezone = "GMT+8")
    private Date time ; //返回时间

//    调用返回成功结果

    /**
     *调用返回成功结果
     * @param result
     * @return success
     * @param <T>
     */
    public static <T> HttpResult<T> success(T result){
        return new HttpResult<>(
                RespCode.SUCCESS.code,
                RespCode.SUCCESS.msg,
                result,
                new Date()
        );
    }

    /**
     *调用返回失败结果
     * @param result
     * @return fail
     * @param <T>
     */

    public static <T> HttpResult<T> failed(T result)
    {
        return new HttpResult<>(
                RespCode.FAILEN.code,
                RespCode.FAILEN.msg,
                result,
                new Date()
        );
    }


    /**
     *调用返回失败结果
     * @return fail
     * @param <T>
     */

    public static <T> HttpResult<T> failed(){
        return failed(null);
    }


}